<?php
/**
 * 分类管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\cn\controllers;

use app\libs\IeltsController;
use app\libs\Method;
use app\modules\cn\models\Info;
use app\modules\content\models\Active;
use app\modules\content\models\Classes;
use app\modules\content\models\Recommend;

class ActiveController extends IeltsController
{
    public $enableCsrfValidation = false;
    public $layout = 'cn';


    public function actionIndex()
    {
        $page = \Yii::$app->request->get('page',1);
        $pageSize = \Yii::$app->request->get('pageSize',8);
        $offset=$pageSize*($page-1);
        $today=date('Y-m-d H:i:s',time());
        $active=Active::find()->asArray()->offset($offset)->limit($pageSize)->orderBy('id desc')->all();//全部 有分页
        $active= Active::getReservation($active);//全部 有分页
        $ing=Active::find()->asArray()->where('startTime<="'.$today.'" and endTime>="'.$today.'"')->orderBy('id desc')->all();//进行中
        $ing=Active::getReservation($ing);//进行中
        $pre=Active::find()->asArray()->where('startTime>"'.$today.'"')->orderBy('id desc')->all();//未开始
        $pre=Active::getReservation($pre);
        $count= count(Active::find()->asArray()->select('id')->orderBy('id desc')->all());
        $countPage= $count!=false?ceil($count/$pageSize):0;
        $pageArray = Method::page($countPage, $page);
        $course = Classes::find()->asArray()->limit(2)->orderBy('viewCount desc')->all();
        $this->title = '热门活动_雅思公开课_雅思一对一-申友雅思';
        $this->keywords = '雅思活动,雅思一对一,雅思评分标准,雅思提高';
        $this->description = '申友雅思热门活动,为广大出国留学考生提供雅思最新雅思评分标准解读,雅思公开课,雅思一对一,雅思提高而开设的一系列优惠活动,只为雅思提高,申友雅思。';
        return $this->render('index', ['course'=>$course,'active'=>$active,'ing'=>$ing,'pre'=>$pre,'pageArray'=>$pageArray]);
    }

    public function actionDetail()
    {
        $id = \Yii::$app->request->get('id');
        $active = Active::find()->asArray()->where('id='. $id)->one();
        if($active!=false){
            Active::updateAll(['viewCount' => ($active['viewCount'] + 1)], "id=$id");
        }
        $time=time();
        $recommend = Info::find()->asArray()->where('pushTime<='.$time)->orderBy('viewCount desc')->limit(4)->all();
        $focus= Recommend::find()->asArray()->where('type=2')->orderBy('sort asc,id desc')->limit(8)->all();
        $this->title = isset($active['name'])?$active['name'].'-申友雅思':'';
        $this->keywords = isset($active['keywords'])?$active['keywords']:'';
        $this->description = isset($active['smallintro'])?$active['smallintro']:'';
        return $this->render('detail', ['active'=>$active,'recommend'=>$recommend,'focus'=>$focus]);
    }


}